package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanException;
import cn.hutool.core.bean.BeanUtil;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.BedVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    @Override
    public void newBed(BedDto bedDto)  {
        Bed bed = BeanUtil.toBean(bedDto , Bed.class);
        bed.setCreateTime(LocalDateTime.now());
        bed.setCreateBy(1L);
        bed.setBedStatus(0);
        try {
            bedMapper.newBed(bed);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.BED_INSERT_FAIL);
        }
    }

    @Override
    public BedVo selectBed(Long id) {
        return bedMapper.selectBed( id);
    }

    @Override
    public void upDateBed(BedDto bedDto) {
        BedVo bedVo = selectBed( bedDto.getId()); //先查这个id 的信息
        bedVo.setSort(bedDto.getSort()); // 房间状态应该是不能被更新的
        bedVo.setBedNumber(bedDto.getBedNumber()); // 房间姓名不能被更新
        if (ObjectUtil.isNotEmpty(bedDto.getBedStatus())) { // 判断BedStatu 是否为空
            bedVo.setBedStatus(bedDto.getBedStatus());
        }
        Bed bed = BeanUtil.toBean(bedVo, Bed.class);
        bed.setCreateTime(LocalDateTime.now());
        bed.setCreateBy(1L);
        bedMapper.upDateBed(bed);
    }

    @Override
    public void deleteBed(Long id) {
        bedMapper.deleteBed(id) ;
    }
}

